package speedchina.controller.statistics;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import speedchina.controller.BaseController;
import speedchina.dto.PageParamDTO;
import speedchina.dto.ProjectStatisticsParamDTO;
import speedchina.dto.UserStatisticsParamDTO;
import speedchina.dto.WeekDTO;
import speedchina.entity.work.ProjectType;
import speedchina.service.statistics.StatisticsService;
import speedchina.service.work.ProjectTypeService;
import speedchina.util.ExcelUtil;
import speedchina.util.WeekUtils;
import speedchina.vo.LayuiTableVO;
import speedchina.vo.statistics.ProjectDetailVO;
import speedchina.vo.statistics.ProjectStatisicsVO;
import speedchina.vo.statistics.UserDetailVO;
import speedchina.vo.statistics.UserStatisicsVO;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


/**
 * 统计
 *
 * @author earthchen
 * @date 2018/10/16
 **/
@RequestMapping("/statistics")
@Controller
public class StatisticsController extends BaseController
{

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private ProjectTypeService projectTypeService;

    private Logger logger = LoggerFactory.getLogger(StatisticsController.class);

    /**
     * 项目工时统计页
     *
     * @return 项目工时统计页
     */
    @RequiresPermissions("statistics:project:view")
    @GetMapping("/project/view")
    public String project(Model model)
    {
        List<ProjectType> projectTypeList = projectTypeService.list(new QueryWrapper<>());
        model.addAttribute("typeList", projectTypeList);
        return "tongji/xmgs_tongji";
    }

    /**
     * 项目工时详情
     *
     * @param model     模型
     * @param projectId 项目编号
     * @return 详情页
     */
    @RequiresPermissions("statistics:project:detailview")
    @GetMapping("/project/view/{projectId}")
    public String projectDetail(Model model, @PathVariable("projectId") String projectId,
                                String projectName,
                                Date startDate,
                                Date endDate)
    {
        model.addAttribute("projectName",projectName);
        model.addAttribute("projectId", projectId);
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        return "tongji/detail/projectDetail";
    }

    /**
     * 分页获取项目工时统计
     *
     * @param dto          项目统计查询参数
     * @param pageParamDTO 分页参数
     * @return 项目统计信息的 layui table
     */
    @RequiresPermissions("statistics:project:list")
    @GetMapping("/project/list")
    @ResponseBody
    public LayuiTableVO<ProjectStatisicsVO> projectData(ProjectStatisticsParamDTO dto,
                                                        PageParamDTO pageParamDTO)
    {
        if (dto.getStartDate() == null || dto.getEndDate() == null)
        {
            WeekDTO weekDTO = WeekUtils.getWeekDays(0);
            dto.setStartDate(weekDTO.getStartDate());
            dto.setEndDate(weekDTO.getEndDate());
        }
        IPage<ProjectStatisicsVO> projectStatisicsVOS = statisticsService.projectStatistics(dto, pageParamDTO);
        return getTableInfo(projectStatisicsVOS);
    }

    /**
     * 项目工时导出excel
     *
     * @param dto      项目统计请求参数
     * @param response 响应体
     */
    @RequiresPermissions("statistics:project:excel")
    @GetMapping("/project/excel")
    public void projectDataToExcel(ProjectStatisticsParamDTO dto,
                                   HttpServletResponse response)
    {
        if (dto.getStartDate() == null || dto.getEndDate() == null)
        {
            WeekDTO weekDTO = WeekUtils.getWeekDays(0);
            dto.setStartDate(weekDTO.getStartDate());
            dto.setEndDate(weekDTO.getEndDate());
        }
        List<ProjectStatisicsVO> projectStatisicsVOS = statisticsService.projectStatistics(dto);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("项目工时统计", "统计"),
            ProjectStatisicsVO.class, projectStatisicsVOS);
        try
        {
            ExcelUtil.export(response, workbook, "项目工时统计.xls");
        }
        catch (Exception e)
        {
            logger.error("excel导出错误");
        }
    }

    /**
     * 项目工时详情
     *
     * @param pageParamDTO 分页参数
     * @param dto          项目请求参数
     * @return 项目详情layui table
     */
    @RequiresPermissions("statistics:project:detail")
    @GetMapping("/project/detail")
    @ResponseBody
    public LayuiTableVO<ProjectDetailVO> projectDetailData(PageParamDTO pageParamDTO,
                                                           ProjectStatisticsParamDTO dto)
    {
        if (dto.getStartDate() == null || dto.getEndDate() == null)
        {
            WeekDTO weekDTO = WeekUtils.getWeekDays(0);
            dto.setStartDate(weekDTO.getStartDate());
            dto.setEndDate(weekDTO.getEndDate());
        }

        IPage<ProjectDetailVO> projectDetailVOS = statisticsService.projectDetail(dto, pageParamDTO);
        return getTableInfo(projectDetailVOS);
    }

    /**
     * 员工工时统计页
     *
     * @return 员工工时统计页
     */
    @RequiresPermissions("statistics:user:view")
    @GetMapping("/user/view")
    public String user()
    {
        return "tongji/yggs_tongji";
    }

    /**
     * 员工工时详情
     *
     * @param model    模型
     * @param username 用户名
     * @return 详情页
     */
    @RequiresPermissions("statistics:user:detailview")
    @GetMapping("/user/view/{username}")
    public String userDetail(Model model, @PathVariable("username") String username,
                             Date startDate, Date endDate)
    {
        model.addAttribute("username", username);
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        return "tongji/detail/userDetail";
    }

    /**
     * 员工工时统计
     *
     * @param pageParamDTO 分页参数
     * @param dto          员工统计参数
     * @return 员工统计 layui table
     */
    @RequiresPermissions("statistics:user:list")
    @GetMapping("/user/list")
    @ResponseBody
    public LayuiTableVO<UserStatisicsVO> userData(PageParamDTO pageParamDTO,
                                                  UserStatisticsParamDTO dto)
    {

        if (dto.getStartDate() == null || dto.getEndDate() == null)
        {
            WeekDTO weekDTO = WeekUtils.getWeekDays(0);
            dto.setStartDate(weekDTO.getStartDate());
            dto.setEndDate(weekDTO.getEndDate());
        }
        IPage<UserStatisicsVO> statisicsVOS = statisticsService.userStatistics(dto, pageParamDTO);
        return getTableInfo(statisicsVOS);
    }

    /**
     * 员工工时导出excel
     *
     * @param dto      员工统计参数
     * @param response 响应
     */
    @RequiresPermissions("statistics:user:excel")
    @GetMapping("/user/excel")
    public void userDataToExcel(UserStatisticsParamDTO dto,
                                HttpServletResponse response)
    {
        if (dto.getStartDate() == null || dto.getEndDate() == null)
        {
            WeekDTO weekDTO = WeekUtils.getWeekDays(0);
            dto.setStartDate(weekDTO.getStartDate());
            dto.setEndDate(weekDTO.getEndDate());
        }
        List<UserStatisicsVO> statisicsVOS = statisticsService.userStatistics(dto);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工工时统计", "统计"),
            UserStatisicsVO.class, statisicsVOS);
        try
        {
            ExcelUtil.export(response, workbook, "员工工时统计");
        }
        catch (Exception e)
        {
            logger.error("excel导出错误");
        }
    }

    /**
     * 员工工时详情
     *
     * @param pageParamDTO 分页参数
     * @param dto          员工统计请求参数
     * @return 员工工时详情 layui table
     */
    @RequiresPermissions("statistics:user:detail")
    @GetMapping("/user/detail")
    @ResponseBody
    public LayuiTableVO<UserDetailVO> userDetailData(PageParamDTO pageParamDTO,
                                                     UserStatisticsParamDTO dto)
    {
        if (dto.getStartDate() == null || dto.getEndDate() == null)
        {
            WeekDTO weekDTO = WeekUtils.getWeekDays(0);
            dto.setStartDate(weekDTO.getStartDate());
            dto.setEndDate(weekDTO.getEndDate());
        }
        IPage<UserDetailVO> userDetailVOS = statisticsService.userDetail(dto, pageParamDTO);
        return getTableInfo(userDetailVOS);
    }
}
